package com.gimi.cloud.bbp.manage.controller.sys;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.sys.SysMenuDTO;
import com.gimi.cloud.bbp.manage.service.sys.SysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理Controller
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-24
 **/
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
    private static final Logger logger = LoggerFactory.getLogger(SysMenuController.class);
    private SysMenuService sysMenuService;
    @Autowired
    public SysMenuController(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }

    /**
     * 根据条件获取菜单列表
     * @param request
     * @param response
     * @param menuDTO 查询参数
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response, @RequestBody SysMenuDTO menuDTO) throws Exception{
        Response resp = new Response();
        List<SysMenuDTO> menuList = sysMenuService.queryList(menuDTO,false);
        Map<String,Object> resData = new HashMap<>();
        resData.put("menus",menuList);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 获取菜单树结构
     * @param request
     * @param response
     * @param menuDTO 查询条件
     * @throws Exception
     */
    @RequestMapping("/tree")
    public Response tree(HttpServletRequest request,HttpServletResponse response,@RequestBody SysMenuDTO menuDTO) throws Exception{
        Response resp = new Response();
        List<SysMenuDTO> menuList = sysMenuService.queryList(menuDTO,false);
        menuList = sysMenuService.menuToTree(menuList);
        Map<String,Object> resData = new HashMap<>();
        resData.put("menus",menuList);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 加载菜单信息
     * @param request
     * @param response
     * @param menuDTO 菜单查询参数
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody SysMenuDTO menuDTO) throws  Exception{
        Response resp = new Response();
        SysMenuDTO menu = sysMenuService.load(menuDTO, menuDTO.getMeId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("menu",menu);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 保存菜单信息
     * @param request
     * @param response
     * @param menuDTO 待保存的菜单信息
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@RequestBody SysMenuDTO menuDTO) throws Exception{
        Response resp = new Response();
        sysMenuService.saveOrUpdate(menuDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"保存成功");
        return resp;
    }

    /**
     * 删除菜单信息
     * @param request
     * @param response
     * @param menuDTO 待删除的菜单信息
     * @throws Exception
     */
    @RequestMapping("/delete")
    public Response delete(HttpServletRequest request,HttpServletResponse response,@RequestBody SysMenuDTO menuDTO) throws  Exception{
        Response resp = new Response();
        sysMenuService.delete(menuDTO,menuDTO.getMeId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }
}
